//
//  ChainBaseController.m
//  Chain
//
//  Created by zhaosh on 2018/3/30.
//  Copyright © 2018年 zhaosh. All rights reserved.
//

#import "ChainBaseController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIImage+Color.h"

@interface ChainBaseController ()<UITabBarControllerDelegate,UITabBarDelegate>
{
    CGFloat _fontSize;
}
@end

@implementation ChainBaseController
- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.navigationController.navigationBar.backgroundColor = HEXCOLOR(@"#ce0d0d");
    self.navigationController.navigationBar.tintColor = HEXCOLOR(@"#ce0d0d");
 
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageFromColor:HEXCOLOR(@"#ce0d0d") forSize:CGSizeMake(ScreenWidth, kTopHeight - 0.5) withCornerRadius:0]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
    UINavigationBar.appearance.tintColor =  HEXCOLOR(@"#ce0d0d");
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor =[UIColor whiteColor];
    
    self.tabBarController.delegate = self;
    
    _fontSize=18;
}

- (int)convertToInt:(NSString*)strtemp
{
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
        
    }
    return strlength;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - 对顶部视图的设置
-(void)setDefaultNavBarWithTitle:(NSString *)title{
    [self setHeadView:self.view withLeftBtnText:@"back_icon" withHeadLable:title withRightBntText:@""];
}

// 自定义导航栏 左、右、中间 各一个按钮
-(void)setHeadView:(UIView *)view withLeftBtnText:(NSString *)leftText withHeadLable:(NSString *)titleText withRightBntText:(NSString *)rightText
{
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    if ([leftText isEqualToString:@""] || !leftText) {
        
    }else if([leftText isEqualToString:@"全选"] || [leftText isEqualToString:@"全不选"] ){
        backBtn.width += 15;
        [backBtn setTitle:leftText forState:UIControlStateNormal];
        backBtn.titleLabel.font = FONT_LB(16);
    }else{
        [backBtn setImage:[UIImage imageNamed:leftText] forState:(UIControlStateNormal)];
    }
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    backBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    
    UIButton *titleViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    [titleViewBtn setTitle:titleText forState:(UIControlStateNormal)];
    [titleViewBtn setTitleColor:[UIColor colorWithHexString:@"#fff"] forState:(UIControlStateNormal)];
    titleViewBtn.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
    [titleViewBtn addTarget:self action:@selector(titleViewBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    titleViewBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.frame = CGRectMake(0, 0, 44, 44);
    if([rightText isEqualToString:@"提交"]|| [rightText isEqualToString:@"编辑"]||[rightText isEqualToString:@"取消"]){
        [rightBtn setTitle:rightText forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        
    }else if(![rightText isEqualToString:@""] && rightText){
        [rightBtn setImage:[UIImage imageNamed:rightText] forState:(UIControlStateNormal)];
        rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        
    }
    [rightBtn addTarget:self action:@selector(addAction) forControlEvents:(UIControlEventTouchUpInside)];
    rightBtn.adjustsImageWhenHighlighted = NO;
    self.rightBtn = rightBtn;
    
//    UIBarButtonItem *rightBtnItem;
//    if([rightText isEqualToString:@"提交"]||
//       [rightText isEqualToString:@"编辑"]||
//       [rightText isEqualToString:@"保存"]||
//       [rightText isEqualToString:@"更新"]||
//       [rightText isEqualToString:@"添加"]||
//       [rightText isEqualToString:@"确定"]||
//       [rightText isEqualToString:@"筛选"]||
//       [rightText isEqualToString:@"清空"]||
//       [rightText isEqualToString:@"完成"]||
//       [rightText isEqualToString:@"操作"]||
//       [rightText isEqualToString:@"取消"]||
//       [rightText isEqualToString:@"多选"]||
//       [rightText isEqualToString:@"指派项目"]||
//       [rightText isEqualToString:@"删除项目"]||
//       [rightText isEqualToString:@"移交项目"]||
//       [rightText isEqualToString:@"分享"])
//    {
//        rightBtnItem  = [[UIBarButtonItem alloc]initWithTitle:rightText style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
//        [rightBtnItem setTitleTextAttributes:@{
//                                               NSFontAttributeName: [UIFont systemFontOfSize:_fontSize]
//                                               } forState:UIControlStateNormal];
//    }else{
//        if ([rightText isEqualToString:@""] || !rightText) {
//
//        }else{
//            rightBtnItem  = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:rightText] style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
//        }
//    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];;
    self.rightBtnItem = self.navigationItem.rightBarButtonItem;
    self.navigationItem.titleView = titleViewBtn;
    
}
// 自定义导航栏 左、右、中间 其中右边两个按钮

- (void)setHeadView:(UIView*)view withLeftBtnTitle:(NSString *)leftText withCenterTitle:(NSString *)titleText withRightBtnView:(NSArray *)array
{
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    if ([leftText isEqualToString:@""] || !leftText) {
        
    }else{
        [backBtn setImage:[UIImage imageNamed:leftText] forState:(UIControlStateNormal)];
    }
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    backBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    //    [headView addSubview:backBtn];
    
    UIButton *titleViewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    titleViewBtn.frame = CGRectMake(0, 0, 100, 44);
    [titleViewBtn setTitle:titleText forState:UIControlStateNormal | UIControlStateHighlighted];
    [titleViewBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:(UIControlStateNormal)];
    titleViewBtn.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
    [titleViewBtn addTarget:self action:@selector(titleViewBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    titleViewBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    [titleViewBtn setImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
    
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:array[0]] style:(UIBarButtonItemStylePlain) target:self action:@selector(searchAction)];
    
    [item1 setImageInsets:UIEdgeInsetsMake(2, 17, -2, -17)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:array[1]] style:(UIBarButtonItemStylePlain) target:self action:@selector(addAction)];
    
    
    NSArray* arr = @[item2,item1];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.rightBarButtonItems = arr;
    
    self.navigationItem.titleView = titleViewBtn;
}

// 自定义导航栏 左、右、中间  中间按钮需要添加图片
-(void)setHeadView:(UIView *)view withLeftBtnText:(NSString *)leftText withTitle:(NSString *)titleText withImage:(NSString *)imageName withRightBntText:(NSString *)rightText
{
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    if ([leftText isEqualToString:@""] || !leftText) {
        
    }else{
        [backBtn setImage:[UIImage imageNamed:leftText] forState:(UIControlStateNormal)];
    }
    
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    backBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    //    [headView addSubview:backBtn];
    
    UIButton *titleViewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    titleViewBtn.frame = CGRectMake(0, 0, 100, 44);
    
    [titleViewBtn setTitle:titleText forState:(UIControlStateNormal)];
    [titleViewBtn setImage:[UIImage imageNamed:imageName] forState:(UIControlStateNormal)];
    [titleViewBtn setTitleColor:[UIColor colorWithHexString:@"#fff"] forState:(UIControlStateNormal)];
    titleViewBtn.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
    [titleViewBtn addTarget:self action:@selector(titleViewBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [titleViewBtn setImagePosition:LXMImagePositionRight spacing:3];
    titleViewBtn.adjustsImageWhenHighlighted = NO;//取消点击时的高亮
    
    UIButton *addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    addBtn.frame = CGRectMake(0, 0, 44, 44);
    if([rightText isEqualToString:@"提交"]){
        [addBtn setTitle:rightText forState:UIControlStateNormal];
        addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        addBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        
    }else{
        [addBtn setImage:[UIImage imageNamed:rightText] forState:(UIControlStateNormal)];
        addBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        
    }
    [addBtn addTarget:self action:@selector(addAction) forControlEvents:(UIControlEventTouchUpInside)];
    addBtn.adjustsImageWhenHighlighted = NO;
    self.rightBtn = addBtn;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addBtn];
    self.navigationItem.titleView = titleViewBtn;
}

#pragma mark - HUD
//展示HUD
-(void)show:(MBProgressHUDMode )_mode message:(NSString *)_message detailMessage:(NSString *)_detailMessage customView:(id)customView {
    
    _HUD = [[MBProgressHUD alloc] initWithView:customView];
    [customView addSubview:_HUD];
    _HUD.mode=_mode;
    _HUD.customView = customView;
    _HUD.animationType = MBProgressHUDAnimationZoom;
    _HUD.label.text = _message;
    _HUD.detailsLabel.text = _detailMessage;
    _HUD.contentColor = [UIColor whiteColor];
    _HUD.bezelView.color = [UIColor blackColor];
    _HUD.removeFromSuperViewOnHide = YES;
    
    [_HUD showAnimated:YES];
}

-(void)show:(MBProgressHUDMode )_mode message:(NSString *)_message customView:(id)customView {
    //    dispatch_async(dispatch_get_main_queue(), ^{
    [self show:_mode message:_message detailMessage:@"" customView:customView];
    //    });
}

-(void)showLoading{
    [self show:MBProgressHUDModeIndeterminate message:@"努力加载中......" customView:self.view];
}

-(void)showLoadingWithMessage:(NSString *)message{
    [self show:MBProgressHUDModeIndeterminate message:message customView:self.view];
}

-(void)showRemindWithMessage:(NSString *)message{
    [self hideHUDafterDelay:0];
    [self show:MBProgressHUDModeText message:message customView:self.view];
    [self hideHUDafterDelay:1.0];
}
// 标题 + 详情 类型的HUD
- (void)showRemindWithMessage:(NSString *)message detailMessage:(NSString *)detailMessage {
    [self hideHUDafterDelay:0];
    [self show:MBProgressHUDModeText message:message detailMessage:detailMessage customView:self.view];
    [self hideHUDafterDelay:1.6];
}

//隐藏HUD
- (void)hideHUDafterDelay:(CGFloat)delay
{
    [_HUD hideAnimated:YES afterDelay:delay];
}

#pragma mark - 网络诊断
-(BOOL)isConnectionAvailable{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //DebugLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //DebugLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //DebugLog(@"3G");
            break;
    }
    return isExistenceNetwork;
}

-(BOOL) isConnectionWIFI{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //DebugLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //DebugLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = NO;
            //DebugLog(@"3G");
            break;
    }
    return isExistenceNetwork;
}


#pragma mark -- public methods

- (void)backAction
{
    
}
- (void)titleViewBtnAction
{
    
}
- (void)addAction
{
    
}
- (void)searchAction
{
    
}

- (void)setupDarkBg{
    UIImageView *bkV = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:bkV];
    bkV.image = [UIImage imageNamed:@"beijing_darkgray"];
    bkV.contentMode = UIViewContentModeScaleToFill;
}
@end

